package indi.zhifa.study2025.oa.service.dep.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.study2025.oa.dao.gen.service.IGenPositionDbService;
import indi.zhifa.study2025.oa.dao.gen.service.IGenPositionTagDbService;
import indi.zhifa.study2025.oa.dao.gen.service.IGenRelPositionTagDbService;
import indi.zhifa.study2025.oa.dao.mybatis.dep.mapper.PositionMapper;
import indi.zhifa.study2025.oa.dao.mybatis.dep.mapperOut.PositionTagMapperOut;
import indi.zhifa.study2025.oa.dao.po.GenPositionEntity;
import indi.zhifa.study2025.oa.dao.po.GenPositionTagEntity;
import indi.zhifa.study2025.oa.dao.po.GenRelPositionTagEntity;
import indi.zhifa.study2025.oa.service.dep.IPositionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class PositionServiceImpl implements IPositionService{

    final IGenPositionDbService mGenPositionDbService;
    final IGenRelPositionTagDbService mGenRelPositionTagDbService;
    final IGenPositionTagDbService mGenPositionTagDbService;
    final PositionMapper mPositionMapper;

    @Override
    public GenPositionEntity check(Long pId) {
        GenPositionEntity positionEntity = mGenPositionDbService.check(pId);
        return positionEntity;
    }

    @Override
    public GenPositionEntity create(GenPositionEntity pGenPositionEntity, List<Long> pTagIds) {

        LambdaQueryWrapper<GenPositionEntity> queryWrapper = new LambdaQueryWrapper<GenPositionEntity>()
                .eq(GenPositionEntity::getCode, pGenPositionEntity.getCode());
        if(mGenPositionDbService.exists(queryWrapper)) {
            throw new ServiceException("已经存在code为"+pGenPositionEntity.getCode()+"的职位");
        }
        if(!mGenPositionDbService.save(pGenPositionEntity)){
            throw new ServiceException("存储GenPositionEntity失败");
        }

        if(!CollectionUtils.isEmpty(pTagIds)){
            relTags(pGenPositionEntity, pTagIds);
        }

        return pGenPositionEntity;
    }


    protected List<GenRelPositionTagEntity> relTags(GenPositionEntity pGenPositionEntity, List<Long> pTagIds) {

        LambdaQueryWrapper<GenPositionTagEntity> positionTagQueryWrapper = Wrappers.<GenPositionTagEntity>lambdaQuery()
                .in(GenPositionTagEntity::getId, pTagIds)
                .select(GenPositionTagEntity::getId,GenPositionTagEntity::getCode);
        List<GenPositionTagEntity> positionTags = mGenPositionTagDbService.list(positionTagQueryWrapper);
        Map<Long,GenPositionTagEntity> positionMap = positionTags.stream().collect(Collectors.toMap(GenPositionTagEntity::getId, v->v));
        if(positionTags.size() < pTagIds.size()){
            List<Long> missIds = pTagIds.stream().filter(depId -> !positionMap.containsKey(depId)).collect(Collectors.toList());
            throw new ServiceException("职位tag Id"+ StrUtil.join(",", missIds)+"数据丢失");
        }

        LambdaUpdateWrapper<GenRelPositionTagEntity> deleteWrapper = Wrappers.<GenRelPositionTagEntity>lambdaUpdate()
                .eq(GenRelPositionTagEntity::getPositionId, pGenPositionEntity.getId());
        mGenRelPositionTagDbService.remove(deleteWrapper);

        List<GenRelPositionTagEntity> relPositionTagEntities = new ArrayList<>();
        for(GenPositionTagEntity positionTagEntity : positionTags){
            GenRelPositionTagEntity relPositionTagEntity = new GenRelPositionTagEntity();
            relPositionTagEntity.createInit();
            relPositionTagEntity.setPositionId(pGenPositionEntity.getId());
            relPositionTagEntity.setPositionCode(pGenPositionEntity.getCode());
            relPositionTagEntity.setTagId(positionTagEntity.getId());
            relPositionTagEntity.setTagCode(positionTagEntity.getCode());
            relPositionTagEntities.add(relPositionTagEntity);
        }
        mGenRelPositionTagDbService.saveBatch(relPositionTagEntities);

        return relPositionTagEntities;
    }

    @Override
    public List<GenRelPositionTagEntity> relTags(Long pPositionId, List<Long> pTagIds) {

        GenPositionEntity pGenPositionEntity = mGenPositionDbService.checkMin(pPositionId);
        return relTags(pGenPositionEntity, pTagIds);
    }

    @Override
    public GenPositionEntity edit(Long pId, Object pGenPositionEntityDto) {
        GenPositionEntity positionEntity = mGenPositionDbService.updatePull(pId, pGenPositionEntityDto);
        return positionEntity;
    }

    @Override
    public GenPositionEntity checkById(Long id) {
        GenPositionEntity positionEntity = mGenPositionDbService.check(id);
        return positionEntity;
    }

    @Override
    public GenPositionEntity delete(Long id) {
        GenPositionEntity delObj = mGenPositionDbService.deleteById(id);
        return delObj;
    }

    @Override
    public List<GenPositionEntity> list() {
        return mGenPositionDbService.list();
    }

    @Override
    public List<PositionTagMapperOut> listByPositionId(Long pPositionId) {
        return mPositionMapper.listByPositionId(pPositionId);
    }
}
